{{extend 'layout.html'}}

<!-- load dojo and provide config via data attribute -->
<script src="{{=URL('static','dojo/dojo/dojo.js')}}" data-dojo-config="async: true, parseOnLoad: true"></script>


<style>
p {
	font-family: Arial,sans-serif;
	font-size: 13px;
	color: rgb(0, 0, 0);
	margin-top: 2px;
	margin-bottom: 2px;
	line-height: 24px;
	/*text-shadow: 0px 0.5px 0px rgb(48, 48, 48);*/
}


.form_grilla_pendientes {
	font-family: "Lucida Grande", sans-serif;
	font-size: 11px;
	position: relative;
}

/* styles for autoheight */
#grid {
	
	height: auto;
}
#grid .dgrid-scroller {
	position: relative;
	overflow-y: hidden;
}


has-ie-6 #grid {
	width: 100%;
	/* Give the grid a height.  Not optimal, but we'd need more JS for better. */
	height: 20em;
}

#grid .dgrid-header-scroll {
	display: none;
}
#grid .dgrid-header {
	right: 0;
}

</style>


<script type="text/javascript">
	
function unique(arr){
	//	create a unique list of items from the passed array
	//	(removing duplicates).  This is quick and dirty.

	//	first, set up a hashtable for unique objects.
	var obj = {};
	for(var i=0,l=arr.length; i<l; i++){
		if(!(arr[i] in obj)){
			obj[arr[i]] = true;
		}
	}

	//	now push the unique objects back into an array, and return it.
	var ret = [];
	for(var p in obj){
		ret.push(p);
	}
	ret.sort();
	return ret;
}	
	
	
	
require(
["dijit/Dialog", "dojo/_base/xhr", "dojo/_base/declare",
"dojo/store/Memory", "dojo/store/Observable", 
"dgrid/Grid", "dgrid/extensions/Pagination",
"dgrid/Selection", "dgrid/Keyboard", "dgrid/GridFromHtml",
"dojo/_base/lang", "dojo/_base/declare", "dojo/dom-construct", 
"dojo/dom-form", "dgrid/test/data/base", "dojo/domReady!"],
function(Dialog, xhr, declare, 
	Memory, Observable, 
	Grid, Pagination, 
	Selection, Keyboard, GridFromHtml,
	lang, declare, domConstruct, domForm){

	var dialog = new Dialog({
        });
	
	
	xhr.get({
			// URL de la request
			url: '{{=URL(r = request, c = "correo", f="correo_mails_consulta_by_from")}}',
			handleAs: "json",
			content: {
				from_: '{{=from_}}'
			},
			load: function(response) {
				cargar_grilla(response.data_list, response.rows);
			}
	});
	
	cargar_grilla = function(data_list, rows){
		

		
	data = {
		identifier: 'id',
		label: 'id',
		items: []
	};
	
	
	for(var i=0, l=data_list.length; i<rows; i++){
		data.items.push(lang.mixin({ id: i }, data_list[i%l]));
	}
	
	// global var testStore
	testStore = Observable(new Memory({data: data}));
	
	var CustomGrid = declare([Grid, Keyboard, Selection, Pagination]);
	
	getColumns2 = function(){
		return {
		From: 'From',
		Subject: 'Subject',
		Fecha: 'Fecha'
		};
	};	
	
	window.grid = new CustomGrid({
		store: testStore,
		columns: getColumns2(),
		pagingLinks: false,
		pagingTextBox: false,
		firstLastArrows: true,
		pageSizeOptions: [10, 30, 50]
		}, "grid");
		

	document.getElementById("iniciarNueva").onclick = function(){
		window.location = "{{=URL(r = request, c = 'comunicacion', f = 'comunicacion_mail')}}/" + "{{=id_mail_bandeja}}";
	};
	
	
	showDialog_datos_mail = function(response){
		var result = '<p><strong>Contenido</strong></p>';
		result += '<div style="height:150px;overflow-y:scroll;border:1px solid #769dc4;padding:0 10px;width:250px">';
	    result += '<label>From: </label>' + " " + response.From;
	    result += '</br>';
	    result += '<label>Subject: </label>' + " " + response.Subject;
	    result += '</br>';
	    result += '<label>Message: </label>' + " " + response.Text;
	       
	    result +=  '</div>';

	    dialog.set("content", result);
		dialog.show();
	}
		
	document.getElementById("verSelected").onclick = function(){
		
		for (var i in grid.selection) {
			
			xhr.get({
					// URL de la request
					url: '{{=URL(r = request, c = "correo", f="correo_load_mail_consulta")}}',
					handleAs: "json",
					// Enviar los datos para checkear en la db 
					content: {
						id_mail : i
					},
					load: function(response) {
						showDialog_datos_mail(response);

					}
			});
		}
	};
	
	document.getElementById("continuarSelected").onclick = function(){
		for (var i in grid.selection) {
			xhr.get({
					url: '{{=URL(r = request, c = "correo", f="correo_resolver")}}',
					handleAs: "json",
					content: {
						mail_bandeja : "{{=id_mail_bandeja}}",
						mail_consulta : i
					},
					load: function(response) {
						window.location = response;
					}
			});
		}
	};
	
	};
	
})
</script>



<button type="button" id="verSelected">Ver</button>
<button type="button" id="iniciarNueva">Iniciar como nueva</button>
<button type="button" id="continuarSelected">Continuar seleccionada</button>

<p>
	En la grilla puede ver las consultas pertenecientes a una {{=T("direccion")}} de correo 
	en particular. 
</p>
<p>
	Si el mail recibido es {{=T("continuacion")}} de uno de los que figura en la lista, puede
	continuar esa misma consulta sin necesidad de volver a cargar todos los datos 
	presionando el {{=T("boton")}} "Continuar seleccionada".
	Si el mail corresponde a una consulta nueva debe elegir la {{=T("opcion")}} "Iniciar como nueva".
</p>

{{=form}}